﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.LIB.YJKControls;

namespace DBService
{
	public partial class DlgChoose : System.Windows.Forms.Form
	{
		public DlgChoose(Autodesk.Revit.DB.Document doc)
		{
			this.mDoc = doc;
			this.InitializeComponent();
			this.txtPath.Text = XmlUtils.GetPathName(doc);
		}

		private void button1_Click(object sender, EventArgs e)
		{
			new DlgConfig().ShowDialog(this);
		}

		private void rbLocal_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbLocal.Checked)
			{
				this.txtPath.Enabled = true;
				this.txtServer.Enabled = false;
				this.txtDatabase.Enabled = false;
				this.txtUserName.Enabled = false;
				this.txtPwd.Enabled = false;
			}
		}

		private void rbService_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbService.Checked)
			{
				this.txtPath.Enabled = false;
				this.txtServer.Enabled = true;
				this.txtDatabase.Enabled = true;
				this.txtUserName.Enabled = true;
				this.txtPwd.Enabled = true;
			}
		}

		public bool IsServer { get; set; }

		public string Path { get; set; }

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.rbService.Checked)
			{
				this.IsServer = true;
			}
			else
			{
				this.IsServer = false;
			}
			this.Path = this.txtPath.Text;
		}

		private void btnChoose_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				this.txtPath.Text = folderBrowserDialog.SelectedPath;
			}
		}

		private void DlgChoose_Load(object sender, EventArgs e)
		{
			if (!new FileInfo(this.fileName).Exists)
			{
				XmlUtils.CreateConfigFile(this.fileName);
				this.txtServer.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Server", "value");
				this.txtDatabase.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "DbName", "value");
				this.txtUserName.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Uid", "value");
				this.txtPwd.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Pwd", "value");
				return;
			}
			this.txtServer.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Server", "value");
			this.txtDatabase.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "DbName", "value");
			this.txtUserName.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Uid", "value");
			this.txtPwd.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Pwd", "value");
		}

		private Document mDoc;

		private string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
	}
}
